using System;
using PluginFrame;

namespace PcrDataAnalysis
{
	public class ManualBaseLineNormalization : BaseLineNormalization, IBaseLineNormalization
	{
		protected double mBaseLineOptK;

		protected double mBaseLineMidPoint;

		protected bool mRotateCurve;

		public ManualBaseLineNormalization(IApplication app)
			: base(app)
		{
			mRotateCurve = true;
		}

		protected override void ChannelNormalization(IPcrCell pcrCell, int channelIndex, bool isAnalyzing, int startBaseLine, int stopBaseLine, double[] channelData)
		{
			if (isAnalyzing)
			{
				bool flag = pcrCell.CellPosition == "H4";
				if (mRotateCurve)
				{
					RotateTranlateData(pcrCell, ref channelData, channelIndex, startBaseLine, stopBaseLine);
				}
				double num = CalculateBaseLineAverage(channelData, startBaseLine, stopBaseLine);
				pcrCell.SetBaseLineAvgValue(channelIndex, num);
				for (int i = 0; i < channelData.Length; i++)
				{
					channelData[i] = Math.Log(channelData[i] / num, 2.0);
				}
				num = CalculateBaseLineAverage(channelData, startBaseLine, stopBaseLine);
				for (int j = 0; j < channelData.Length; j++)
				{
					channelData[j] -= num;
					pcrCell.SetNormalLightData(channelIndex, j + 1, channelData[j]);
				}
			}
		}

		protected void RotateTranlateData(IPcrCell pcrCell, ref double[] channelLightData, int channelIndex, int startBaseLine, int stopBaseLine)
		{
			if (stopBaseLine != startBaseLine)
			{
				if (stopBaseLine - 1 < channelLightData.Length)
				{
					mBaseLineOptK = (channelLightData[stopBaseLine - 1] - channelLightData[startBaseLine - 1]) / (double)(stopBaseLine - startBaseLine);
					mBaseLineMidPoint = Convert.ToInt32((double)(stopBaseLine + startBaseLine) / 2.0) - 1;
				}
				else
				{
					mBaseLineOptK = 0.0;
					mBaseLineMidPoint = 0.0;
				}
				double num = 0.0;
				for (int i = 0; i < channelLightData.Length; i++)
				{
					channelLightData[i] = channelLightData[i] - ((double)i - mBaseLineMidPoint) * mBaseLineOptK - num;
				}
			}
		}

		public new AnalysisError Execute(IPcrCell pcrCell, IAbsoluteAnalysisParameterConfiguration analysisOption, int channelIndex, bool isAnalyzing)
		{
			mRotateCurve = isAnalyzing;
			return base.Execute(pcrCell, analysisOption, channelIndex, isAnalyzing);
		}
	}
}
